﻿using System.Data.Entity;
using System.Linq;
using ZBJF.Warranty.Model;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceProductSellerProductsSnapshootService
    {
        /// <summary>
        /// 产品订单的产品快照（保险产品非保险卡）
        /// </summary>
        /// <param name="insuranceProductOrderID">销售订单ID</param>
        /// <param name="insuranceProductOrderNo">订单号</param>
        /// <param name="sellerInsuranceProductID">保险卡产品</param>
        /// <returns></returns>
        public bool CreateSnapshoot(long insuranceProductOrderID, string insuranceProductOrderNo,
            long sellerInsuranceProductID)
        {
            var sellerProduct = ServiceFactory.SellerInsuranceProductsServiceInstance.LoadById(sellerInsuranceProductID);
            if (sellerProduct != null)
            {
                var tagNames =
                    ServiceFactory.SellerInsuranceProductTagsServiceInstance.LoadBySellerInsuranceProductId(
                        sellerInsuranceProductID).ToList().Aggregate(string.Empty, (c, d) => c + ";" + d.TagName).Trim(';');
                var imagePaths =
                    ServiceFactory.SellerInsuranceProductImagesServiceInstance.LoadInsuranceProductImages(
                        sellerInsuranceProductID).ToList().Aggregate(string.Empty, (c, d) => c + ";" + d.FilePath).Trim(';');
                var insuranceProductOrderSellerProductsSnapshoot = new InsuranceProductSellerProductsSnapshoot
                {
                    SellerInsuranceProductID = sellerInsuranceProductID,
                    SellerId = sellerProduct.SellerId,
                    SellerInsuranceProductName = sellerProduct.ProductName,
                    IsCardProduct = sellerProduct.IsCardProduct,
                    Intro = sellerProduct.Intro,
                    SaleRetailPrice = sellerProduct.SaleRetailPrice,
                    InsuranceCompanyRetailPrice = sellerProduct.InsuranceCompanyRetailPrice,
                    ZBJFSalePrice = sellerProduct.ZBJFSalePrice,
                    MaxSalesQty = sellerProduct.MaxSalesQty,
                    IdentityCardLimitQty = sellerProduct.IdentityCardLimitQty,
                    IsAvailable = sellerProduct.IsAvailable,
                    CreateBy = sellerProduct.CreateBy,
                    CreateTime = sellerProduct.CreateTime,
                    LastModifyBy = sellerProduct.LastModifyBy,
                    LastModifyTime = sellerProduct.LastModifyTime,
                    TagNames = tagNames,
                    ImagesPath = imagePaths,
                };
                return AddEntities(insuranceProductOrderSellerProductsSnapshoot).ID > 0;
            }
            return true;
        }

        public IQueryable<InsuranceProductSellerProductsSnapshoot> GetSellerProductsSnapshoot(
            string insuranceProductOrderNo,
            long insuranceProductID)
        {
            return
               LoadEntities(
                   o =>
                       true).AsNoTracking();
        }
    }
}